<?php
/**
 *
 * @author    Lucian Daia < lucian.daia@live.com >
 * @copyright 2010, Lucian Daia
 * @package   KChunk
 */
class KChunk_HttpProbe
{
	/**
	 * @var string
	 */
	private $_resourceUrl = null;

	/**
	 * @var array
	 */
	private $_resourceData = array();

	/**
	 * Constructor
	 * @param string $resourceUrl
	 */
	public function __construct($resourceUrl)
	{
		$this->_resourceUrl = $resourceUrl;
	}

	/**
	 * Attempts to find the content length (in bytes) of an online resource
	 *
	 * @return int
	 */
	public function getResourceContentLength()
	{
		if (array_key_exists('contentLength', $this->_resourceData)) {
			return $this->_resourceData['contentLength'];
		}

		$client = new Zend_Http_Client($this->_resourceUrl, array(
			'maxredirects' => 0,
			'timeout'      => 30));

		try {
			// in order to get the content lenth, just do a HEAD request
			// to the server, so no traffic is wasted
			$response      = $client->request('HEAD');
			$contentLength = $response->getHeader('Content-Length');
			return $contentLength;
		} catch (Exception $e) {
			echo $e->getMessage();
			return null;
		}
	}
}